package member.controller;

import java.sql.SQLException;

import member.model.domain.Member;
import member.model.service.MemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MemberController {
	
	private MemberService service;
	@Autowired
	public MemberController(MemberService service){
		this.service=service;
	}
	
	/***********************************************************************************
	 *                                                         회원가입 처리 Control
	 ***********************************************************************************/
	
	@RequestMapping("/member/join_form.do")
	public String toJoinForm(){
		return "/member/join.jsp";
	}
	
	@RequestMapping("/member/id_check.do")
	public String joinIdCheck(String id, ModelMap map) throws SQLException{
		boolean flag = service.idCheck(id);
		map.addAttribute("duplicate", flag);
		return "jsonView";
	}
	
	@RequestMapping("/member/join.do")
	public String join(String memberId, String memberPassword, String email) throws SQLException{
		Member member = new Member(memberId, memberPassword, email);
		service.joinMember(member);
		return "/index.jsp";
	}
	
	
	/***********************************************************************************
	 *                                                              로그인 처리 Control
	 ***********************************************************************************/
	
	public String loginIdCheck(String id, ModelMap map) throws SQLException{
		String msg = null;
		
		boolean flag = service.loginCheck(id);
		if(!flag){
			msg = "가입된 아이디가 아니군요!";
		}else{
			msg = id+"님 반갑습니다.";
		}
		map.addAttribute("msg", msg);
		
		return "jsonView";
	}
	
	
	
}
